package org.vincen.helloworld.poi;

import java.io.OutputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class QuerystatImpl {

	public int export2Excel(OutputStream bosStream, List<Object[]> list) {
		int length = 0;
		try {
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFSheet sheet = workbook.createSheet();
			// 注意编码，否则出现乱码，poi包为poi-2.5.1-final-20040804.jar
			// poi.jar包没有该方法设置中文编码
			workbook.setSheetName(0, "案件列表");
			if (list == null || list.isEmpty())// 待导出的数据为空
				return 0;
			for (int i = 0; i < list.size(); i++) {
				HSSFRow hr = sheet.createRow((short) (i));
				Object[] objs = (Object[]) list.get(i);
				int j = 0;
				while (true) {
					HSSFCell hc = hr.createCell(j);
					if (hc == null)
						continue;
					if (objs[j] instanceof String) {
						hc.setCellType(HSSFCell.CELL_TYPE_STRING);
						hc.setCellValue(new HSSFRichTextString(objs[j]
								.toString()));
					} else if (objs[j] instanceof Boolean) {
						hc.setCellType(HSSFCell.CELL_TYPE_BOOLEAN);
						hc.setCellValue((Boolean) objs[j]);
					} else if (objs[j] instanceof Number) {
						hc.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
						hc.setCellValue((Double) objs[j]);
					} else if (objs[j] instanceof Number) {
						hc.setCellType(HSSFCell.CELL_TYPE_BLANK);
						hc.setCellValue(new HSSFRichTextString(""));
					}
					if (++j == objs.length)
						break;
				}
			}
			// 自动调整列宽@author郑保川
			for (int i = 0; i < list.get(0).length; i++) {
				sheet.autoSizeColumn((short) i);
			}

			workbook.write(bosStream);
			length = workbook.getBytes().length;
			bosStream.flush();
			bosStream.close();
		} catch (Exception e) {
			e.printStackTrace();
			return length;
		}
		return length;
	}

	public int export2Excel(OutputStream bosStream, String sheetName,
			List<Object[]> list) {
		int length = 0;
		try {
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFSheet sheet = workbook.createSheet();
			// 注意编码，否则出现乱码，poi包为poi-2.5.1-final-20040804.jar
			// poi.jar包没有该方法设置中文编码
			workbook.setSheetName(0, sheetName);
			if (list == null || list.isEmpty())// 待导出的数据为空
				return 0;
			for (int i = 0; i < list.size(); i++) {
				HSSFRow hr = sheet.createRow((short) (i));
				Object[] objs = (Object[]) list.get(i);
				int j = 0;
				while (true) {
					HSSFCell hc = hr.createCell(j);
					if (hc == null)
						continue;
					if (objs[j] instanceof String) {
						hc.setCellType(HSSFCell.CELL_TYPE_STRING);
						hc.setCellValue(new HSSFRichTextString(objs[j]
								.toString()));
					} else if (objs[j] instanceof Boolean) {
						hc.setCellType(HSSFCell.CELL_TYPE_BOOLEAN);
						hc.setCellValue((Boolean) objs[j]);
					} else if (objs[j] instanceof Number) {
						hc.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
						hc.setCellValue((Double) objs[j]);
					} else if (objs[j] instanceof Number) {
						hc.setCellType(HSSFCell.CELL_TYPE_BLANK);
						hc.setCellValue(new HSSFRichTextString(""));
					}
					if (++j == objs.length)
						break;
				}
			}
			// 自动调整列宽@author郑保川
			for (int i = 0; i < list.get(0).length; i++) {
				sheet.autoSizeColumn((short) i);
			}
			workbook.write(bosStream);
			length = workbook.getBytes().length;
			bosStream.flush();
			bosStream.close();
		} catch (Exception e) {
			e.printStackTrace();
			return length;
		}
		return length;
	}
}
